#ifndef _ResourceMgr_h_
#define _ResourceMgr_h_

#include "SDL.h"
#include "SFont.h"
#include <vector>
#include <string>
#include <iostream>
using namespace std;

class ResourceMgr {

	private:
		enum ResourceType {
			RESOURCE_GFX,
			RESOURCE_FONT,
			RESOURCE_FREE  // Not allocated
		};

		struct Resource {
			string id;
			ResourceType type;
			void *data;
			Resource () :
				id(""), data(NULL), type(RESOURCE_FREE) {}
			~Resource () {
				switch (type) {
					case RESOURCE_GFX:
						cout << ">>> Freeing GFX resource: " << id << endl;
						SDL_FreeSurface ((SDL_Surface *)data);
						break;
					case RESOURCE_FONT:
						cout << ">>> Freeing Bitmap font resource: " << id << endl;
						SFont_FreeFont ((SFont_Font *)data);
						break;
					default:
						cout << "(WW) Unhandled resource type: " << type << endl;
						break;
				}
				id = string("");
				data = NULL;
				type = RESOURCE_FREE;
			}
		};


	public:
		static ResourceMgr *instance () {
			if (instance_ == NULL) {
				instance_ = new ResourceMgr;
		 }
			return instance_;
		}

		static void kill () {
			if (instance_ != NULL) {
        cout << "[Killed resource manager]" << endl;
				delete instance_;
				instance_ = NULL;
			}
		}

		void setRootPath (const string& path) {
			path_ = string(path);
		}

		// Find a resource
		Resource *find (const string& id);

		// Destroy a resource
		void destroy (const string& id);
		void destroy (Resource *resource);

		// Load an image
		// The optional parameter "cache" specifies if the
		// surface needs to be wrapped in a Resource and added
		// to the cache or not. Default is yes (true)
		// The optional parameter "no_dispformat" prevents the function
		// to convert the graphics to the display format if set to true.
		SDL_Surface *getGfx (const string& name, bool cache = true, bool no_dispformat = false);

		// Load an SFont font
		SFont_Font *getFont (const string& name);

	private:
		ResourceMgr  () {}
		~ResourceMgr ();

	private:
		static ResourceMgr *instance_;
		string path_;

  // Cache
		vector<Resource *> resources_;

};

#endif

